package team.se.tms.controller.teach_course;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.constant.SessionConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.CourseDetail;
import team.se.tms.dto.Page;
import team.se.tms.dto.TaskDetail;
import team.se.tms.dto.TeachingDetail;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.ProjectInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.po.UserInfo;
import team.se.tms.service.ProjectInfoService;
import team.se.tms.service.SelectCourseInfoService;
import team.se.tms.service.TaskInfoService;
import team.se.tms.service.TeachCourseInfoService;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.KeyUtil;
import team.se.tms.util.RandomUtil;
import team.se.tms.util.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 授课信息控制层
 *
 * @author winkyle
 * @date 2018/12/7
 */
@Slf4j
@Controller
@RequestMapping(value = "/teach_course")
public class TeachCourseInfoController extends BaseController {
    @Resource(name = "TeachCourseInfoServiceImpl")
    private TeachCourseInfoService teachCourseInfoService;
    @Resource(name = "SelectCourseInfoServiceImpl")
    private SelectCourseInfoService selectCourseInfoService;
    @Resource(name = "TaskInfoServiceImpl")
    private TaskInfoService taskInfoService;
    @Resource(name = "ProjectInfoServiceImpl")
    private ProjectInfoService projectInfoService;
    /**
     * 访问我的选课页面
     * 
     * @return String 页面地址
     * @create on 2018/12/7 by winkyle
     */
    @RequestMapping(value = "/view_my_select_course")
    public String viewMySelectCourse(){
        return "select_course/select_course_list";
    }

    /**
     * 访问所有授课页面
     *
     * @return
     * @throws Exception
     * @create on 2018/12/8 by qingx
     */
    @RequestMapping("list")
    public String viewTeachCourse() throws Exception{
        return "teach_course/teach_course_list";
    }

    /**
     * 获取课程详细信息
     *
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2018/12/7 by winkyle
     */
    @RequestMapping(value = "/search_course_details")
    @ResponseBody
    public Object searchCourseDetails(Page page) throws Exception {
        log.debug("=======================page{}",page.toString());
        return page.getDataTabelJson(teachCourseInfoService.listCourseDetails(((UserInfo) Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId(), page));
    }

    /**
     * 获取所有授课的详细信息
     *
     * @param page 分页
     * @return json字符串
     * @throws Exception 异常
     * @create on 2019/1/3 by qingx
     */
    @RequestMapping(value = "/search_teach_course_details")
    @ResponseBody
    public Object searchAllCourseDetails(Page page,HttpSession httpSession) throws Exception {
        log.debug("=======================page{}",page.toString());
        List<CourseDetail> courseDetails = teachCourseInfoService.listAllCourseDetails(page);
        httpSession.setAttribute("courseDetails",courseDetails);
        return page.getDataTabelJson(courseDetails);
    }

    /**
     * 访问选课详情界面
     *
     * @param courseDetail 选课详情
     * @return ModelAndView 页面模板
     * @throws Exception 异常
     * @create on 2019/1/3 by winkyle
     */
    @RequestMapping(value = "/to_select_course_detail")
    @CheckFormat({"teachCourseIcon","courseInfo","teacherInfo","stageInfos","stageChain","labelLibInfo"})
    public ModelAndView toSelectCourseDetail(CourseDetail courseDetail) throws Exception {
        log.debug("========================CourseDetail{}",courseDetail.toString());
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("courseDetail",courseDetail);
        modelAndView.addObject("courseGrade",selectCourseInfoService.getCourseGrade(((UserInfo)Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId(),courseDetail.getTeachCourseId()));
        modelAndView.setViewName("select_course/select_course_details/select_course_details");
        return modelAndView;
    }

    /**
     * 访问授课详情界面
     *
     * @param courseDetail 授课详情
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/3 by qingx
     */
    @RequestMapping(value = "/to_teach_course_detail")
    public ModelAndView toTeachCourseDetail(CourseDetail courseDetail) throws Exception {
        log.debug("========================CourseDetail{}",courseDetail.toString());
        ModelAndView modelAndView = getModelAndView();
        modelAndView.addObject("courseDetail",courseDetail);
        modelAndView.setViewName("teach_course/teach_course_detail/teach_course_detail");
        return modelAndView;
    }

    /**
     * 选课详情tab页切换
     * @param teachCourseId 选课id
     * @param page tab页名字
     * @return ModelAndView 页面模板
     * @throws Exception 异常
     * @create on 2019/1/3 by winkyle
     */
    @RequestMapping(value = "/select_course_detail_tab/{teachCourseId}/{page}")
    public ModelAndView selectCourseDetailTab(@SpecialCheck("Pk")@PathVariable("teachCourseId")String teachCourseId, @PathVariable("page") String page) throws Exception {
        log.debug("=========================page{}",page);
        System.out.println("____________________"+page);
        ModelAndView modelAndView = getModelAndView();
        if (page.equals("my_project")){

            String userId = ((UserInfo) Jurisdiction.getSession().getAttribute(SessionConstant.USER)).getUserId();
            //teachCourseId = "20181208232425OL";
            List<ProjectInfo> projectInfoList = projectInfoService.listProjectInfo(teachCourseId,userId);
            System.out.println("%%%%%%%%%%%"+projectInfoList);
            modelAndView.addObject("projectInfoList",projectInfoList);
            modelAndView.setViewName("project/my_project");

        }
        modelAndView.addObject("teachCourseId",teachCourseId);
        modelAndView.setViewName("select_course/select_course_details/"+page);
        return modelAndView;
    }


    /**
     * 授课详情tab页切换
     * @param teachCourseId 选课id
     * @param page tab页名字
     * @return ModelAndView 页面模板
     * @throws Exception 异常
     * @create on 2019/1/3 by qingx
     */
    @RequestMapping(value = "/teach_course_detail_tab/{teachCourseId}/{page}")
    public ModelAndView teachCourseDetailTab(@PathVariable("teachCourseId")String teachCourseId,@PathVariable("page") String page) throws Exception {
        log.debug("=========================page{}",page);
        ModelAndView modelAndView = getModelAndView();
        //如果tab页是任务
        if (page.equals("task_list")){
            List<TaskDetail> taskDetails = taskInfoService.listTaskDetails(teachCourseId);
            System.out.println("!!!!!!!!!!!!!!!!!!taskDetails"+taskDetails);
            modelAndView.addObject("taskDetails",taskDetails);
        }
        modelAndView.addObject("teachCourseId",teachCourseId);
        modelAndView.setViewName("teach_course/teach_course_detail/"+page);
        return modelAndView;
    }


    /**
     * 跳转到修改授课页面
     *
     * @param teachCourseId 授课id
     * @param session
     * @return
     * @throws Exception
     * @create on 2018/12/12 by qingx
     */
    @RequestMapping("toModifyTeachCourse")
    @ResponseBody
    public ModelAndView searchModifyTeachingDetail(@Regex("^[A-Za-z0-9]{16}$")String teachCourseId, HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<CourseDetail> courseDetails = (List<CourseDetail>)session.getAttribute("courseDetails");
        CourseDetail courseDetail = new CourseDetail();
        for(CourseDetail item: courseDetails){
            if(item.getTeachCourseId().equals(teachCourseId)){
                courseDetail = item;
                break;
            }
        }
        System.out.println("!!!!!!!!!!!!!!!!!!courseDetail"+courseDetail);
        TeachingDetail teachingDetail = teachCourseInfoService.getModifyTeachingDetail(teachCourseId);
        System.out.println("!!!!!!!!!!!!!!!!!!teachingDetail"+teachingDetail);
        mv.addObject("teachingDetail",teachingDetail);
        //将授课信息返回给界面
        mv.addObject("courseDetail",courseDetail);
        Gson gson =new Gson();
        String selectCourseStudnetsJson = gson.toJson(teachingDetail.getSelectCourseStudents());
        mv.addObject("selectCourseStudnetsJson",selectCourseStudnetsJson);
        mv.setViewName("teach_course/modify_teach_course");
        return mv;
    }


    /**
     * 修改授课信息
     * @param teachCourseInfo 授课信息
     * @param stuIds 授课信息
     * @return
     * @throws Exception
     * @create on 2018/12/18 by qingx
     */
    @RequestMapping(value = "editTeachCourseInfo",method = RequestMethod.POST)
    @CheckFormat
    @ResponseBody
    public Object editTeachCourseInfo(TeachCourseInfo teachCourseInfo,@RequestParam(value="stuIds[]")String[] stuIds) throws Exception{
        System.out.println("111111111teachCourseInfo"+teachCourseInfo);
        boolean result = teachCourseInfoService.modifyTeachCourseInfo(teachCourseInfo,stuIds);
        if (!result){
            throw new UpdateException("授课信息修改失败");
        }
        return  ResultUtil.success("授课信息修改成功");
    }


    /**
     * 跳转到创建授课页面
     *
     * @param session
     * @return
     * @throws Exception
     * @create on 2018/12/18 by qingx
     */
    @RequestMapping("toCreateTeachCourse")
    @ResponseBody
    public ModelAndView searchCreateTeachingDetail(HttpSession session) throws Exception{
        ModelAndView mv = this.getModelAndView();
        TeachingDetail teachingDetail = teachCourseInfoService.getCreateTeachingDetail();
        System.out.println("!!!!!!!!!!!!!!!!!!teachingDetail"+teachingDetail);
        mv.addObject("teachingDetail",teachingDetail);
        mv.setViewName("teach_course/create_teach_course");
        return mv;
    }


    /**
     * 添加授课信息
     *
     * @param stuIds 授课信息
     * @return
     * @throws Exception
     * @create on 2018/12/27 by qingx
     */
    @RequestMapping(value = "addTeachCourseInfo",method = RequestMethod.POST)
    @CheckFormat({"teachCourseId","teachCourseIcon"})
    @ResponseBody
    public Object addTeachCourseInfo(TeachCourseInfo teachCourseInfo,@RequestParam(value="stuIds[]")String[] stuIds) throws Exception{
        System.out.println("111111111teachCourseInfo"+teachCourseInfo);
        boolean result = teachCourseInfoService.saveTeachCourseInfo(teachCourseInfo,stuIds);
        if(result == true){
            return  ResultUtil.success("授课信息添加成功");
        }
        throw new UpdateException("授课信息添加失败");
    }

}
